package com.ruoyi.blog.service.impl;

import java.beans.Transient;
import java.util.List;

import com.ruoyi.blog.config.ResourcesConfig;
import com.ruoyi.blog.dto.config.ConfigForm;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.blog.mapper.TBlogConfigMapper;
import com.ruoyi.blog.domain.TBlogConfig;
import com.ruoyi.blog.service.ITBlogConfigService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 博客基本配置Service业务层处理
 * 
 * @author yang
 * @date 2021-11-28
 */
@Service
public class TBlogConfigServiceImpl implements ITBlogConfigService 
{
    @Autowired
    private TBlogConfigMapper tBlogConfigMapper;

    @Autowired
    private ResourcesConfig resourcesConfig;

    /**
     * 查询博客基本配置
     * 
     * @param configId 博客基本配置主键
     * @return 博客基本配置
     */
    @Override
    public TBlogConfig selectTBlogConfigByConfigId(Long configId)
    {
        return tBlogConfigMapper.selectTBlogConfigByConfigId(configId);
    }

    /**
     * 查询博客基本配置列表
     * 
     * @param tBlogConfig 博客基本配置
     * @return 博客基本配置
     */
    @Override
    public List<TBlogConfig> selectTBlogConfigList(TBlogConfig tBlogConfig)
    {
        return tBlogConfigMapper.selectTBlogConfigList(tBlogConfig);
    }

    /**
     * 新增博客基本配置
     * 
     * @param tBlogConfig 博客基本配置
     * @return 结果
     */
    @Override
    public int insertTBlogConfig(TBlogConfig tBlogConfig)
    {
        return tBlogConfigMapper.insertTBlogConfig(tBlogConfig);
    }

    /**
     * 修改博客基本配置
     * 
     * @param tBlogConfig 博客基本配置
     * @return 结果
     */
    @Override
    public int updateTBlogConfig(TBlogConfig tBlogConfig)
    {
        return tBlogConfigMapper.updateTBlogConfig(tBlogConfig);
    }

    /**
     * 批量删除博客基本配置
     * 
     * @param configIds 需要删除的博客基本配置主键
     * @return 结果
     */
    @Override
    public int deleteTBlogConfigByConfigIds(Long[] configIds)
    {
        return tBlogConfigMapper.deleteTBlogConfigByConfigIds(configIds);
    }

    /**
     * 删除博客基本配置信息
     * 
     * @param configId 博客基本配置主键
     * @return 结果
     */
    @Override
    public int deleteTBlogConfigByConfigId(Long configId)
    {
        return tBlogConfigMapper.deleteTBlogConfigByConfigId(configId);
    }

    /**
     * 后台博客配置更新
     *
     * @param configForm
     * @return
     */
    @Override
    public boolean updateBlogConfig(ConfigForm configForm) {
        TBlogConfig tBlogConfig = new TBlogConfig(null,"maxImageSrc",configForm.getMaxImageSrc());
        tBlogConfigMapper.updateTBlogConfigByKey(tBlogConfig);
        tBlogConfig = new TBlogConfig(null,"maxTitle",configForm.getMaxTitle());
        tBlogConfigMapper.updateTBlogConfigByKey(tBlogConfig);
        tBlogConfig = new TBlogConfig(null,"maxTime",configForm.getMaxTime());
        tBlogConfigMapper.updateTBlogConfigByKey(tBlogConfig);
        tBlogConfig = new TBlogConfig(null,"userBjsrc",configForm.getUserBjsrc());
        tBlogConfigMapper.updateTBlogConfigByKey(tBlogConfig);
        tBlogConfig = new TBlogConfig(null,"userUsersrc",configForm.getUserUsersrc());
        tBlogConfigMapper.updateTBlogConfigByKey(tBlogConfig);
        tBlogConfig = new TBlogConfig(null,"userUsername",configForm.getUserUsername());
        tBlogConfigMapper.updateTBlogConfigByKey(tBlogConfig);
        tBlogConfig = new TBlogConfig(null,"userDetail",configForm.getUserDetail());
        tBlogConfigMapper.updateTBlogConfigByKey(tBlogConfig);
        tBlogConfig = new TBlogConfig(null,"images",configForm.getImages());
        tBlogConfigMapper.updateTBlogConfigByKey(tBlogConfig);
        return true;
    }

    @Override
    public List<TBlogConfig> selectTBlogConfigListByType(Integer configType) {
        List<TBlogConfig> res = tBlogConfigMapper.selectTBlogConfigListByType(configType);
        for(TBlogConfig config:res){
            // xxx1.jpg ==> http://地址/xxx1.jpg
            if(config.getConfigKey().equals("maxImageSrc")){
                config.setConfigValue(resourcesConfig.getFullPath(config.getConfigValue()));
                continue;
            }
            if(config.getConfigKey().equals("userBjsrc")){
                config.setConfigValue(resourcesConfig.getFullPath(config.getConfigValue()));
                continue;
            }
            if(config.getConfigKey().equals("userUsersrc")){
                config.setConfigValue(resourcesConfig.getFullPath(config.getConfigValue()));
                continue;
            }
            // [xxx1.jpg,xxx2.jpg]  ==> [http://地址/xxx1.jpg,http://地址/xxx2.jpg]
            if(config.getConfigKey().equals("images")){
                StringBuilder images = new StringBuilder();
                for(String image:config.getConfigValue().split(",")){
                    images.append(resourcesConfig.getFullPath(image));
                    images.append(",");
                }
                images.deleteCharAt(images.length() - 1);
                config.setConfigValue(images.toString());
                continue;
            }
        }
        return res;
    }

    /**
     * 增加访问量
     *
     * @return
     */
    @Override
    public int addTraffic() {
        return setAddAddOrDeleteDelete("traffic",1);
    }

    /**
     * 增加博客数量
     *
     * @return
     */
    @Override
    public int addblogCounts() {
        return setAddAddOrDeleteDelete("blogCounts",1);
    }

    /**
     * 减少博客数量
     *
     * @return
     */
    @Override
    public int deleteblogCounts() {
        return setAddAddOrDeleteDelete("blogCounts",-1);
    }

    /**
     * 处理增加和减少
     *
     * @param key  key值
     * @param num  增加多少或减少多少
     * @return
     */
    @Override
    public int setAddAddOrDeleteDelete(String key,int num){
        TBlogConfig tBlogConfig = new TBlogConfig();
        tBlogConfig.setConfigKey(key);
        int res = 0;
        try{
            TBlogConfig select = tBlogConfigMapper.selectTBlogConfigList(tBlogConfig).get(0);
            select.setConfigValue(String.valueOf(Integer.valueOf(select.getConfigValue()) + num));
            res = tBlogConfigMapper.updateTBlogConfigByKey(select);
        }catch (IndexOutOfBoundsException exception){
            exception.printStackTrace();
        }
        return res;
    }
}
